package dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import dao.NewsDao;
import dao.pojo.News;

public class NewsDaoImpl extends BaseDaoImpl implements NewsDao{
	
	

	public void addNews(News news) {
		// TODO 自动生成方法存根
		Connection conn = getPool().getConnection();
		
		PreparedStatement pstmt = null;
	    String sql = "INSERT INTO news(title,submit_time,contents)VALUES(?,?,?,?)";
        try
        {
        	pstmt = conn.prepareStatement(sql);
        	pstmt.setString(1, news.getTitle());
        	pstmt.setTimestamp(2, new Timestamp(news.getSubmitTime().getTime()));
        	pstmt.setString(3, news.getContent());
        	pstmt.executeUpdate();
        }catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			getPool().freeConnection(conn);
		}
	}

	public void deleteNews(News news) {
		// TODO 自动生成方法存根
		Connection conn = getPool().getConnection();
		
		PreparedStatement pstmt = null;
		String sql = "DELETE FROM news WHERE id=?";
		try
		{
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, news.getId());
			pstmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		finally
		{
			getPool().freeConnection(conn);
		}
	}

	public ArrayList<News> getAllNews() {
		// TODO 自动生成方法存根
		Connection conn = getPool().getConnection();
		
		ArrayList<News> newsList = new ArrayList<News>();
		PreparedStatement pstmt = null;
		String sql = "SELECT * FROM news";
		ResultSet rs = null;
		try
		{
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				News news = new News();
				news.setId(rs.getInt("id"));
				news.setTitle(rs.getString("title"));
				news.setSubmitTime(new Date(rs.getTimestamp("submit_time").getTime()));
				news.setContent(rs.getString("contents"));
				newsList.add(news);
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		finally
		{
			getPool().freeConnection(conn);
		}
		
		return newsList;
	}

	public News getNews(News news) {
		// TODO 自动生成方法存根
		Connection conn = getPool().getConnection();
		
		News db_news = null;
		PreparedStatement pstmt = null;
		String sql = "SELECT * FROM news WHERE id=?";
		ResultSet rs = null;
		try
		{
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, news.getId());
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				db_news = new News();
				db_news.setId(rs.getInt("id"));
				db_news.setTitle(rs.getString("title"));
				db_news.setSubmitTime(new Date(rs.getTimestamp("submit_time").getTime()));
				db_news.setContent(rs.getString("contents"));				
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		finally
		{
			getPool().freeConnection(conn);
		}
		return db_news;
	}

	public void saveNews(News news) {
		// TODO 自动生成方法存根
		Connection conn = getPool().getConnection();
		
		PreparedStatement pstmt = null;
		String sql = "UPDATE news SET title=?,submit_time=?,contents=? WHERE id=?";
		try
		{
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, news.getTitle());
			pstmt.setTimestamp(2,new Timestamp(news.getSubmitTime().getTime()));
			pstmt.setString(3, news.getContent());
			pstmt.setInt(4, news.getId());
			pstmt.executeUpdate();
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		finally
		{
			getPool().freeConnection(conn);
		}
	}

	public void deleteNewsbyTitle(News news) {
		// TODO 自动生成方法存根
		Connection conn = getPool().getConnection();
		
		PreparedStatement pstmt = null;
		String sql = "DELETE FROM news WHERE title=?";
		try
		{
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, news.getTitle());
			pstmt.executeUpdate();
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		finally
		{
			getPool().freeConnection(conn);
		}
		
		
	}
	
	

}
